package com.qf.czh.vhr.Service;

import com.qf.czh.vhr.Mapper.HrMapper;
import com.qf.czh.vhr.entity.Hr;
import com.qf.czh.vhr.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HrService implements UserDetailsService {
    @Autowired
    HrMapper hrMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Hr hr = hrMapper.loadUserByUsername(username);
        if (hr== null){
            throw  new UsernameNotFoundException("用户不存在");
        }

        //在hr中加入了roles属性,这返回hr之前就要把登录用户的roles查出来赋值上去.
       List<Role> list =  hrMapper.getHrRoleByHrId(hr.getId());
        hr.setRoles(list);
        return hr;
    }
}
